package com.shwread.android.qysw.activity.psychological;

import android.app.Activity;
import android.app.DialogFragment;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

import com.shwread.android.qysw.visitMap.ToastUtil;
import com.shwread.android.qysw10000242.R;

/**
 * 卡号密码Dialog
 */
public class AccountPasswordDialog extends DialogFragment {
    /**
     * 按键监听
     */
    private ClickListener listener;
    /**
     * 帐号
     */
    private CharSequence AccStr;
    /**
     * 密码
     */
    private CharSequence PwdStr;

    private TextView ContentTv;
    private TextView PhoneTv;
    private EditText PhoneEt;
    private TextView OkTv;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Window window = getDialog().getWindow();
        if (window != null) {
            window.setBackgroundDrawableResource(android.R.color.transparent);
        }
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);//去除自带标题栏
        View customView = inflater.inflate(R.layout.dialog_account_password_view, container, false);
        initView(customView);
        initData();
        initListener();
        return customView;
    }

    private void initListener() {
        OkTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    String phoneStr = PhoneEt.getText().toString();
                    if (TextUtils.isEmpty(phoneStr) || phoneStr.length() < 11) {
                        ToastUtil.show(getActivity(), "请输入正确的手机号码");
                        return;
                    }
                    listener.onClick(phoneStr);
                }
                dismiss();
            }
        });
    }

    private void initData() {
        //设置对话框内容
        if (TextUtils.isEmpty(AccStr)) {
            ContentTv.setText("请输入手机号，用于接收评测问卷登录帐号和密码。");
            ContentTv.setTextColor(getResources().getColor(R.color.text_black_4));
            PhoneTv.setText("手机号码");
            PhoneEt.setVisibility(View.VISIBLE);
        } else {
            ContentTv.setTextColor(getResources().getColor(R.color.color_252525));
            ContentTv.setText(String.format("评测帐号：%s", AccStr));
            PhoneTv.setText(String.format("评测密码：%s", PwdStr));
            PhoneEt.setVisibility(View.GONE);
        }
    }

    private void initView(View customView) {
        ContentTv = (TextView) customView.findViewById(R.id.dialog_content_tv);
        PhoneTv = (TextView) customView.findViewById(R.id.dialog_phone_tv);
        PhoneEt = (EditText) customView.findViewById(R.id.dialog_phone_et);
        OkTv = (TextView) customView.findViewById(R.id.dialog_ok_tv);
    }

    public interface ClickListener {
        void onClick(String msgPhone);
    }

    /**
     * 按钮点击事件
     */
    public AccountPasswordDialog setOnClickListener(ClickListener rightListener) {
        this.listener = rightListener;
        return this;
    }

    /**
     * 设置对话框内容
     */
    public AccountPasswordDialog setContent(CharSequence AccStr, CharSequence PwdStr) {
        this.AccStr = AccStr;
        this.PwdStr = PwdStr;
        return this;
    }

    /**
     * 点击其他地方关闭
     */
    public AccountPasswordDialog setCancel(boolean cancelable) {
        super.setCancelable(cancelable);
        return this;
    }

    public void show(Activity activity, String tag) {
        FragmentManager manager = activity.getFragmentManager();
        FragmentTransaction mFragTransaction = manager.beginTransaction();
        Fragment fragment = manager.findFragmentByTag(tag);
        if (fragment != null) {
            //为了不重复显示dialog，在显示对话框之前移除正在显示的对话框
            mFragTransaction.remove(fragment);
        }
        mFragTransaction.commit();
        super.show(manager, tag);
    }
}
